﻿Array.prototype.removeByValue = function (propertyName, targetValue) {
    var v_delIndex = -1;
    for (var i = 0; i < this.length; i++) {
        if (this[i][propertyName] == targetValue) {
            v_delIndex = i;
            break;
        }
    }

    if (v_delIndex > -1) {
        this.splice(v_delIndex, 1);
    }
}
Array.prototype.hasValue = function (propertyName, targetValue) {
    var v_delIndex = -1;
    if (propertyName != null && propertyName != '') {
        for (var i = 0; i < this.length; i++) {
            if (this[i][propertyName] == targetValue) {
                v_delIndex = i;
                break;
            }
        }
        if (v_delIndex > -1) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == targetValue) {
                v_delIndex = i;
                break;
            }
        }
        if (v_delIndex > -1) {
            return true;
        }
        else {
            return false;
        }
    }
}
Array.prototype.getItemByValue = function (propertyName, targetValue) {
    var v_delIndex = -1;
    for (var z = 0; z < this.length; z++) {
        if (this[z][propertyName] == targetValue) {
            v_delIndex = z;
            break;
        }
    }
    if (v_delIndex > -1) {
        this[v_delIndex].OwnIndex = v_delIndex;
        return this[v_delIndex];
    }
    else {
        return null;
    }
}
Array.prototype.findMutliByValue = function (propertyName, targetValue) {
    var retArr = [];
    for (var i = 0; i < this.length; i++) {
        if (this[i][propertyName] == targetValue) {
            retArr.push(this[i]);
        }
    }
    return retArr;
}
Array.prototype.findMutliByValueArr = function (sourceArrItemCol, targetArr, targetArrItemColToFind) {
    var retArr = [];
    for (var i = 0; i < this.length; i++) {
        var sourceItem = this[i];
        for (var j = 0; j < targetArr.length; j++) {
            var targetItem = targetArr[j];

            if (sourceItem[sourceArrItemCol] == targetItem[targetArrItemColToFind]) {
                retArr.push(sourceItem);
                break;
            }
        }
    }
    return retArr;
}
Array.prototype.sortByProperty = function (propertyName, isDesc) {
    var compare = function (obj1, obj2) {
        var val1 = obj1[propertyName];
        var val2 = obj2[propertyName];
        if (val1 < val2) {
            return isDesc === true ? 1 : - 1;
        } else if (val1 > val2) {
            return isDesc === true ? -1 : 1;
        } else {
            return 0;
        }
    }
    this.sort(compare);
}
Array.prototype.getMaxValueItem = function (propertyName) {
    if (propertyName == null || propertyName == undefined) {
        var maxItem = this[0];
        for (var i = 0; i < this.length; i++) {
            if (this[i] > maxItem) {
                maxItem = this[i];
            }
        }
        return maxItem;
    }
    else {
        var maxValue = this[0][propertyName];
        var maxItem = this[0];
        for (var i = 0; i < this.length; i++) {
            if (this[i][propertyName] > maxValue) {
                maxItem = this[i];
                maxValue = this[i][propertyName];
            }
        }
        return maxItem;
    }
}
Array.prototype.getMinValueItem = function (propertyName) {
    if (propertyName == null || propertyName == undefined) {
        var minItem = this[0];
        for (var i = 0; i < this.length; i++) {
            if (this[i] < minItem) {
                minItem = this[i];
            }
        }
        return minItem;
    }
    else {
        var minValue = this[0][propertyName];
        var minItem = this[0];
        for (var i = 0; i < this.length; i++) {
            if (this[i][propertyName] < minValue) {
                minItem = this[i];
                minValue = this[i][propertyName];
            }
        }
        return minItem;
    }
    
}